package com.itchen.school.common.utils;

import com.itchen.school.common.exception.SysException;
import org.springframework.core.io.Resource;

import java.lang.reflect.InvocationTargetException;
import java.util.function.Function;

public class BeanUtils {

    public static Function<Class, Object> beanSupplier;
    public static Function<String, Object> beanSupplierByString;
    public static Function<String, Resource[]> resourceLoaderFunc;

    public BeanUtils() {}

    public static <B> B getBean(Class<B> beanClass) {
        if (beanSupplier != null) {
            return (B) beanSupplier.apply(beanClass);
        } else {
            try {
                return beanClass.getDeclaredConstructor().newInstance();
            } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException | InstantiationException var2) {
                throw new SysException("unable to get instance for " + beanClass.getName());
            }
        }
    }

    public static <B> B getBean(String beanName) {
        if (beanSupplierByString != null) {
            return (B) beanSupplierByString.apply(beanName);
        } else {
            throw new SysException("unable to get instance for " + beanName);
        }
    }

}
